package com.study.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.study.model.dto.ForgetPasswordSetNewPasswordDto;
import com.study.model.dto.ForgetPasswordValidateUserDto;
import com.study.model.dto.ForgetPasswordValidateVerifyCodeDto;
import com.study.model.dto.request.*;
import com.study.model.dto.response.UserInfoRespDto;
import com.study.model.entity.User;
import com.study.service.UserService;
import com.study.util.result.ApiResult;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/register")
    public ApiResult register(@RequestBody UserRegisterDto registerDto) {
        userService.userRegister(registerDto);
        return ApiResult.SUCCESS();
    }

    @PostMapping("/login")
    public ApiResult signIn(@RequestBody UserLoginDto loginDto) {
        User user = userService.login(loginDto);
        //设置loginID和登录设备
        StpUtil.setLoginId(user.getId(), loginDto.getDevice());
        SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
        return ApiResult.SUCCESS(tokenInfo);
        //return ApiResult.SUCCESS(user);
    }

    @PostMapping("/forget/validate_user")
    public ApiResult forgetValidateUser(@RequestBody ForgetPasswordValidateUserDto forgetPasswordValidateUserDto) {
        String token = userService.forgetValidateUser(forgetPasswordValidateUserDto);
        return ApiResult.SUCCESS(token);
    }

    @PostMapping("/forget/validate_verify_code")
    public ApiResult forgetValidateVerifyCode(@RequestBody ForgetPasswordValidateVerifyCodeDto forgetPasswordValidateVerifyCodeDto) {
        userService.forgetValidateVerifyCode(forgetPasswordValidateVerifyCodeDto);
        return ApiResult.SUCCESS();
    }

    @PostMapping("/forget/new_password")
    public ApiResult forgetSetNewPassword(@RequestBody ForgetPasswordSetNewPasswordDto forgetPasswordSetNewPasswordDto) {
        userService.forgetSetNewPassword(forgetPasswordSetNewPasswordDto);
        return ApiResult.SUCCESS();
    }

    @PostMapping("/user/setting")
    public ApiResult updateUser(@RequestBody UserSettingDto userSettingDto) {
        String userId = StpUtil.getLoginIdAsString();
        userSettingDto.setUserId(userId);
        userService.changeNickNameAndPassword(userSettingDto);
        return ApiResult.SUCCESS();
    }

    @PostMapping("/user/info")
    public ApiResult userInfo() {
        String userId = StpUtil.getLoginIdAsString();
        UserInfoRespDto respDto = userService.getUserInfo(userId);
        return ApiResult.SUCCESS(respDto);
    }

    @PostMapping("/logout")
    public ApiResult signOut() {
        log.info("用户：{} 安全退出系统，设备为：{}", StpUtil.getLoginId(), StpUtil.getLoginDevice());
        StpUtil.logout();
        return ApiResult.SUCCESS();
    }

}
